﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ViewModel;

namespace IssueTracker.CustomControls
{
    public partial class issueDetails : UserControl
    {
        Issue _item = null;
        public MainWindow objMain = null;
        private bool refresh = false;
        public issueDetails()
        {
            InitializeComponent();
        }

        public issueDetails(Issue item)
        {
            InitializeComponent();
            _item = item;
        }

        private void issueDetails_Load(object sender, EventArgs e)
        {
            //set background color
            //if (_item.Priority != Priority.Low)
            //    this.BackColor = System.Drawing.ColorTranslator.FromHtml(_item.Priority.GetColor());
            _item = Service.Issues.GetIssueDetails(_item.ID);
            lblIssueName.Text = _item.DisplayText;
            txtDesc.Text = _item.Description;
            lblPriority.Text = _item.Priority.ToString();
            lblDateFrom.Text = _item.DateStart.ToShortDateString();
            lblDateTo.Text = _item.DateEnd.ToShortDateString();
            lblCreatedOn.Text = _item.DateCreated.ToShortDateString();
            cmbStatus.DataSource = Service.NewIssue.GetStatusOptions();
            cmbStatus.DisplayMember = "Name";
            cmbStatus.ValueMember = "ID";
            cmbStatus.SelectedValue = (int)_item.Status;
            lblAssignTo.Text = _item.Assignee.FirstOrDefault().Name;
            if (_item.IsRequest)
            {
                txtValue.Text = _item.UDF1.ToString();
                txtValue.Enabled = true;
            }
            if (_item.DateCompleted.HasValue)
                lblDateCompleted.Text = _item.DateCompleted.Value.ToShortDateString();
            listBoxComments.DataSource = _item.Comments;
            listBoxComments.DisplayMember = "DisplayText";
            objMain.AcceptButton = btnSaveComment;
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            objMain.LoadIssueList(refresh);
        }

        private void btnSaveComment_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if (string.IsNullOrEmpty(txtComment.Text))
            {
                errorProvider1.SetIconAlignment(btnSaveComment, ErrorIconAlignment.MiddleLeft);
                errorProvider1.SetError(btnSaveComment, "Comment can not be empty.");
                return;
            }
            Cursor.Current = Cursors.WaitCursor;
            Service.Issues.PostComment(_item.ID, txtComment.Text);
            _item = Service.Issues.GetIssueDetails(_item.ID);
            listBoxComments.DataSource = _item.Comments;
            listBoxComments.DisplayMember = "DisplayText";
            txtComment.Text = string.Empty;
            Cursor.Current = Cursors.Default;
        }

        private void btnChangeStatus_Click(object sender, EventArgs e)
        {
            UpdateIssueValue();
        }

        private void UpdateIssueValue()
        {
            lblMessage.Visible = false;
            Cursor.Current = Cursors.WaitCursor;
            int value = string.IsNullOrEmpty(txtValue.Text) ? 0 : Convert.ToInt32(txtValue.Text);
            int chk = Service.Issues.UpdateIssueValue(_item.ID, (Status)cmbStatus.SelectedValue, value);
            if (chk > 0)
            {
                lblMessage.Text = "Success";
                lblMessage.ForeColor = Color.Green;
                refresh = true;
            }
            else
            {
                lblMessage.Text = "Fail";
                lblMessage.ForeColor = Color.Red;
            }
            lblMessage.Visible = true;
            Cursor.Current = Cursors.Default;
        }

        private void cmbStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            // ChangeIssueStatus();
        }
    }
}
